<?php
if (!defined('WEB_ROOT')) {
	exit;
}

$productsPerRow = 2;
$productsPerPage = 6;

//$productList    = getProductList($catId);
$children = array_merge(array($catId), getChildCategories(NULL, $catId));
$children = ' (' . implode(', ', $children) . ')';

$sql = "SELECT pd_id, pd_name, pd_price, pd_thumbnail, pd_qty, c.cat_id
		FROM tbl_product pd, tbl_category c
		WHERE pd.cat_id = c.cat_id AND pd.cat_id IN $children 
		ORDER BY pd_name";
$result     = dbQuery(getPagingQuery($sql, $productsPerPage));
$pagingLink = getPagingLink($sql, $productsPerPage, "c=$catId");
$numProduct = dbNumRows($result);


// the product images are arranged in a table. to make sure
// each image gets equal space set the cell width here
$columnWidth = (int)(100 / $productsPerRow);
?>
<table width="100%" border="0" cellspacing="1" cellpadding="4" align="center">
<?php 
if ($numProduct > 0 ) {

	$i = 0;
	while ($row = dbFetchAssoc($result)) {
	
		extract($row);
		if ($pd_thumbnail) {
			$pd_thumbnail = WEB_ROOT . 'images/product/' . $pd_thumbnail;
		} else {
			$pd_thumbnail = WEB_ROOT . 'images/no-image-small.png';
		}
	
		if ($i % $productsPerRow == 0) {
			echo '<tr>';
		}

		// format how we display the price
		$pd_price = displayAmount($pd_price);
		
		/*echo "

			<tr  rowspan = '2' >
				<td width=\"$columnWidth%\" align=\"center\">
					<a href=\"" . $_SERVER['PHP_SELF'] . "?c=$catId&p=$pd_id" . "\">
						<img src=\"$pd_thumbnail\" border=\"0\" width=\"108\" height=\"101\">
					</a> 
				</td>
				
			</tr>	
			

				<td>
				</td>	
				<td width=\"$columnWidth%\" align=\"center\">
					<a href=\"" . $_SERVER['PHP_SELF'] . "?c=$catId&p=$pd_id" . "\">$pd_name
					</a> <br>Price : $pd_price
				</td>

			

		";*/
	
		echo "<td><table width=\"100%\" height=\"125\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
  				<tr> 
    				<td width=\"10%\" rowspan=\"3\">&nbsp;</td>
    				<td width=\"17%\" rowspan=\"3\">
						<a href=\"" . $_SERVER['PHP_SELF'] . "?c=$catId&p=$pd_id" . "\">
						<img src=\"$pd_thumbnail\" border=\"0\" width=\"75\" height=\"75\">
						</a> 
					</td>
   				 	<td width=\"10%\">&nbsp;</td>
    				<td width=\"90%\" align=\"center\">
						<h4>$pd_name </h4>
					</a>
					</td>
   	 				<td width=\"10%\" rowspan=\"3\">&nbsp;</td>
  					</tr>
  					<tr> 
    					<td>&nbsp;</td>
    					<td> 
							<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
        						<tr> 
          							<td colspan=\"2\" align = \"center\" Style=\"font-size: 12px;\">Our Price/One Unit </td>
        						</tr>
        						<tr> 
          							<td colspan = \"2\" align = \"center\">$pd_price</td>
          							<td>&nbsp;</td>
        						</tr>
      						</table>
						</td>
  					</tr>
  					<tr> 
    				<td height=\"36\">&nbsp;
					</td>
    				<td>
						<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
        					<tr> 
			  					<td width=\"1%\" align=\"left\" valign=\"middle\"></td>
          	 					<td width=\"24%\" align=\"right\" valign=\"middle\"><a href=\"" . $_SERVER['PHP_SELF'] . "?c=$catId&p=$pd_id" . "\"><img src=\"resource/images/info.gif\" /></a></a></td>
          						<td width=\"68%\" align=\"right\" valign=\"middle\"><input type=\"button\" name=\"btnAddToCart\" value=\"Add To Cart &gt;\" onClick=\"window.location.href='cart.php?action=add&p=$pdId'\" class=\"addToCartButton\"></td>
          						<td width=\"7%\" align=\"right\" valign=\"middle\"></td>
        					</tr>
      					</table>
	  				</td>
  				</tr>
  				<tr> 
    				<td>&nbsp;</td>
    				<td>&nbsp;</td>
    				<td height=\"24\">&nbsp;</td>
    				<td>&nbsp;</td>
    				<td>&nbsp;</td>
  				</tr>
			</table> </td>
			
			";
		
		// if the product is no longer in stock, tell the customer
		if ($pd_qty <= 0) {
			echo "<br>Out Of Stock";
		}
		
		echo "</td>\r\n ";
	
		if ($i % $productsPerRow == 0) {
			echo "<td  width=\"1\"><img src=\"resource/images/bodybg.jpg\" alt=\"\" width=\"1\" height=\"100\"/></td>";
		}
		
		if ($i % $productsPerRow == $productsPerRow - 1) {
			echo '</tr>';
		}
		
		$i += 1;
	}
	
	if ($i % $productsPerRow > 0) {
		echo '<td colspan="' . ($productsPerRow - ($i % $productsPerRow)) . '">&nbsp;</td>';
	}
	
} else {
?>
	<tr><td width="100%" align="center" valign="center">No products in this category</td></tr>
<?php	
}	
?>
</table>
<p align="center"><?php echo $pagingLink; ?></p>